package com.zbfmq.web;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/3/11  8:15
 * 描述:
 */
@RestController
public class TestMQController2 {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("testHeader")
    public void sendHeader(){

       // map.put("zhangsan","123");
       // map.put("goodsId","456");

        MessageProperties messageProperties=new MessageProperties();
        messageProperties.getHeaders().put("zhangsan","123");
        messageProperties.getHeaders().put("goodsId","456");

        Message message=new Message("hello my name is header".getBytes(),messageProperties);

        rabbitTemplate.send("HEADER-1",null,message);
    }


    @RequestMapping("guoqi")
    public void testGuoQi(){
        rabbitTemplate.convertAndSend("ZHI-LIAN-1","zhengchang-key","1234566");
    }

    /**
     * 作者: LCG
     * 日期: 2021/3/11  9:00
     * 描述: 配置消息的过期时间
     * @Param []
     * @Return void
     */
    @RequestMapping("guoq2")
    public void testGuoQi2(){

        MessageProperties messageProperties=new MessageProperties();
        //过期时间
        messageProperties.setExpiration("5000");
        Message message=new Message("123456".getBytes(),messageProperties);



        rabbitTemplate.convertAndSend("ZHI-LIAN-1","zhengchang-key",message);
    }

   /**
    * 作者: LCG
    * 日期: 2021/3/11  9:44
    * 描述: 演示     Confirm
    * @Param []
    * @Return void
    */
    @RequestMapping("guoq3")
    public void testGuoQi3(){

        MessageProperties messageProperties=new MessageProperties();
        //过期时间
        messageProperties.setExpiration("5000");
        Message message=new Message("123456".getBytes(),messageProperties);
        CorrelationData correlationData=new CorrelationData();
        correlationData.setId("111111111111111");
        rabbitTemplate.convertAndSend("ZHI-LIAN-1","zhengchang-key",message,correlationData);
    }



    /**
     * 作者: LCG
     * 日期: 2021/3/11  13:26
     * 描述: 测试手动消费确认
     * @Param []
     * @Return void
     */
    @RequestMapping("test")
    public void test006(){


        rabbitTemplate.convertAndSend("test-exhange","test-key","123456 !");
    }


    @Autowired
    private AmqpAdmin amqpAdmin;

    @RequestMapping("addqueue/{name}")
    public void testAddQueue(@PathVariable String name){
        amqpAdmin.declareExchange(new DirectExchange(name,false,true));
        amqpAdmin.declareQueue(new Queue(name,false,false,true));
        amqpAdmin.declareBinding(new Binding(name,null,name,name,null));
    }

}
